﻿using Mangdot.Webchat.Models;
using RazorEngine.Configuration;
using RazorEngine.Templating;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace Mangdot.Webchat.Common
{
    public class RazorHelper
    {
        public static string CompileHtml(Models.HKModel model)
        {
            var config = new TemplateServiceConfiguration();
            using (var service = RazorEngineService.Create(config))
            {
                string path1 = HttpContext.Current.Server.MapPath("~/Template/cshtml/001.cshtml");
                string index = System.IO.File.ReadAllText(path1, System.Text.Encoding.UTF8);
                string result = service.RunCompile(index, string.Empty, null, model);

                string outputFilePath = string.Format("{0}/CoustomHK", AppDomain.CurrentDomain.BaseDirectory);
                Directory.CreateDirectory(outputFilePath);//创建目录
                File.WriteAllText(outputFilePath + "/" + model.Id + ".html", result, System.Text.Encoding.UTF8);//写入文件

                return "/CoustomHK/" + model.Id + ".html";
            }
        }

        public static string CompileHtml(string viewHtml, Models.HKModel model, string savePath, string fileName)
        {
            var config = new TemplateServiceConfiguration();
            using (var service = RazorEngineService.Create(config))
            {
                string result = service.RunCompile(viewHtml, string.Empty, null, model);

                Directory.CreateDirectory(savePath);//创建目录

                File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "/" + fileName, result, System.Text.Encoding.UTF8);//写入文件

                return fileName;
            }
        }

        public static string CompileHtml(string viewHtml, List<CardsModel> list, DynamicViewBag v)
        {
            var config = new TemplateServiceConfiguration();
            using (var service = RazorEngineService.Create(config))
            {
                string result = service.RunCompile(viewHtml, string.Empty, null, list, v);

                string savePath = AppDomain.CurrentDomain.BaseDirectory + "/HEKA";
                if (!Directory.Exists(savePath))
                {
                    Directory.CreateDirectory(savePath);//创建目录
                }
                File.WriteAllText(savePath + "/index.html", result, System.Text.Encoding.UTF8);//写入文件

                return "";
            }
        }
    }
}